#include "MCDrawCircleCommand.h"
#include "MCCircle2D.h"
#include "MCCircleVisual.h"
#include "MCLineSegment2D.h"
#include "MCPoint2D.h"


MCDrawCircleCommand::MCDrawCircleCommand(MCCommandManager *owner) :
    MCCommand(owner),
    _currentStage(MCDrawCircleStage::SetCenter),
    _parametricWindow(nullptr)
{
    _circleVisual = new MCCircleVisual(presenter, MCCircle2D());
    presenter->addVisualItem(_circleVisual);
}

MCDrawCircleCommand::~MCDrawCircleCommand()
{
    if (_parametricWindow != nullptr)
    {
        delete _parametricWindow;
    }
}

void MCDrawCircleCommand::onMouseDown(IMouseArgs &args)
{
    if (args.isLeftMousePressed())
    {
        if (_currentStage == MCDrawCircleStage::SetCenter)
        {
            _settedCenter = args.globalPos();
            _circleVisual->update(_settedCenter, 0);
            _currentStage = MCDrawCircleStage::SetRadius;
        }
        else if(_currentStage == MCDrawCircleStage::SetRadius)
        {
            _settedRadius = MCLineSegment2D(_settedCenter, args.globalPos()).length();
            _circleVisual->update(_settedCenter, _settedRadius);
            _currentStage = C_ParametricWindow;
            
            _parametricWindow = new MCCircleParametricWindow(this, _circleVisual);
            _parametricWindow->show();
        }
    }
    else if (args.isRightMousePressed())
    {
       terminate(); 
    }
} 

void MCDrawCircleCommand::onMouseUp(IMouseArgs &args)
{

}

void  MCDrawCircleCommand::onMouseMove(IMouseArgs &args)
{
    if (_currentStage == MCDrawCircleStage::SetRadius)
    { 
        _settedRadius = MCLineSegment2D(_settedCenter, args.globalPos()).length();
        _circleVisual->update(_settedCenter, _settedRadius);
    }
}

void MCDrawCircleCommand::terminate()
{
    presenter->removeVisualItem(_circleVisual);
}
